layui.use(['table', 'admin', 'ax', 'func', 'element', 'layer'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var element = layui.element;
    var layer = layui.layer;


    //一些事件监听
    element.on('tab(tabBrief)', function (data) {
        // console.log(data);
        $('#dataStandardName').val("");
        $('#dataStandardDetail').val("");
        $('#dataCleanRuleNumber').val("");
        $('#dataCleanRuleName').val("");
        if (data.index == 0) {
            table.reload(StandardManagement.tableId, {
                where: {}, page: {curr: 1}
            });
            ids = [];
        }
        if (data.index == 1) {
            table.reload(CleanRule.tableId, {
                where: {}, page: {curr: 1}
            });
            ids1 = []
        }
    });

    /**
     * 数据标准管理表管理
     */
    var StandardManagement = {
        tableId: "standardManagementTable"
    };

    /**
     * 数据清洗规则表管理
     */
    var CleanRule = {
        tableId: "cleanRuleTable"
    };

    /**
     * 初始化数据标准管理表格的列
     */
    StandardManagement.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'dataStandardId', hide: true, title: '主键id'},
            {field: 'dataStandardNumber', sort: false, title: '标准编号', width: 165, align: "center"},
            {field: 'dataStandardName', sort: false, title: '标准名称', width: 225, align: "center"},
            {field: 'dataStandardDetail', sort: false, title: '标准描述', align: "center"},
            {field: 'dataIsDelete', sort: false, hide: true, title: '1代表可用；0代表删除'},
            {align: 'center', toolbar: '#tableBar', hide: true, title: '操作'}
        ]];
    };

    /**
     * 初始化数据清洗规则表格的列
     */
    CleanRule.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'dataCleanRuleId', hide: true, title: '主键id'},
            {field: 'dataCleanRuleNumber', sort: false, title: '数据清洗规则编号', width: 165, center: 'align'},
            {field: 'dataCleanRuleName', sort: false, title: '数据清洗规则名称'},
            {field: 'dataIsDelete', sort: false, hide: true, title: '1代表可用；0代表删除'},
            {align: 'center', toolbar: '#tableBar', hide: true, title: '操作'}
        ]];
    };


    /**
     * 数据标准管理点击查询按钮
     */
    StandardManagement.search = function () {
        var queryData = {};

        queryData['dataStandardName'] = $('#dataStandardName').val();
        queryData['dataStandardDetail'] = $('#dataStandardDetail').val();

        table.reload(StandardManagement.tableId, {
            where: queryData, page: {curr: 1}
        });

        ids = []
    };

    /**
     * 数据清洗规则点击查询按钮
     */
    CleanRule.search = function () {
        var queryData = {};

        queryData['dataCleanRuleNumber'] = $('#dataCleanRuleNumber').val();
        queryData['dataCleanRuleName'] = $('#dataCleanRuleName').val();

        table.reload(CleanRule.tableId, {
            where: queryData, page: {curr: 1}
        });

        ids1 = []
    };


    /**
     * 标准管理弹出添加对话框
     */
    StandardManagement.openAddDlg = function () {
        layer.open({
            title: '添加数据标准管理表',
            content: Feng.ctxPath + '/standardManagement/add',
            tableId: StandardManagement.tableId,
            type: 2,
            area: ['80%', '80%'],
            end: function () {
                table.reload(StandardManagement.tableId, {
                    where: {}, page: {curr: 1}
                });
                ids = []
            }
        });
    };

    /**
     * 清洗规则弹出添加对话框
     */
    CleanRule.openAddDlg = function () {
        layer.open({
            title: '添加数据清洗规则表',
            content: Feng.ctxPath + '/cleanRule/add',
            tableId: CleanRule.tableId,
            type: 2,
            area: ['80%', '80%'],
            end: function () {
                table.reload(CleanRule.tableId, {
                    where: {}, page: {curr: 1}
                });
                ids1 = []
            }
        });
    };


    /**
     * 标准管理点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    $("#btnEdit").click(function () {
        if (ids.length != 1) {
            Feng.error("请选择一条数据")
            return;
        }
        layer.open({
            title: '修改数据标准管理表',
            content: Feng.ctxPath + '/standardManagement/edit?dataStandardId=' + ids[0],
            tableId: StandardManagement.tableId,
            type: 2,
            area: ['80%', '80%'],
            end: function () {
                table.reload(StandardManagement.tableId, {
                    where: {}, page: {curr: 1}
                });
                ids = []
            }
        });
    })

    /**
     * 清洗规则点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    $("#btnEdit1").click(function () {
        if (ids1.length != 1) {
            Feng.error("请选择一条数据")
            return;
        }
        layer.open({
            title: '修改数据清洗规则表',
            content: Feng.ctxPath + '/cleanRule/edit?dataCleanRuleId=' + ids1[0],
            tableId: CleanRule.tableId,
            type: 2,
            area: ['80%', '80%'],
            end: function () {
                table.reload(CleanRule.tableId, {
                    where: {}, page: {curr: 1}
                });
                ids1 = []
            }
        });
    })


    /**
     * 数据标准管理删除
     */
    $("#btnDelete").click(function () {
        if (ids.length == 0) {
            Feng.error("请至少选择一条数据")
            return;
        }
        if (ids.length == 1) {
            var operation = function () {
                $.ajax(Feng.ctxPath + '/standardManagement/deleteDataStandard', {
                    data: {
                        arr: ids
                    },
                    //开启序列化
                    traditional: true,
                    success: function (res) {
                        // console.log(res)
                        if (res == ids.length) {
                            Feng.success('删除成功')
                            table.reload(StandardManagement.tableId);
                        }
                    },
                    error: function () {
                        Feng.error('删除失败')
                    }
                })
            };
            Feng.confirm("是否删除这条数据?", operation);
        }
        if (ids.length > 1) {
            var operation = function () {
                $.ajax(Feng.ctxPath + '/standardManagement/deleteDataStandard', {
                    data: {
                        arr: ids
                    },
                    //开启序列化
                    traditional: true,
                    success: function (res) {
                        // console.log(res)
                        if (res == ids.length) {
                            Feng.success('删除成功')
                            table.reload(StandardManagement.tableId);
                        }
                    },
                    error: function () {
                        Feng.error('删除失败')
                    }
                })
            };
            Feng.confirm("是否删除这" + ids.length + "条数据?", operation);
        }
    })


    /**
     * 清洗规则删除
     */
    $("#btnDelete1").click(function () {
        if (ids1.length == 0) {
            Feng.error("请至少选择一条数据")
            return;
        }
        if (ids1.length == 1) {
            var operation = function () {
                $.ajax(Feng.ctxPath + '/cleanRule/deleteCleanRule', {
                    data: {
                        arr: ids1
                    },
                    //开启序列化
                    traditional: true,
                    success: function (res) {
                        // console.log(res)
                        if (res == ids1.length) {
                            Feng.success('删除成功')
                            table.reload(CleanRule.tableId);
                        }
                    },
                    error: function () {
                        Feng.error('删除失败')
                    }
                })
            };
            Feng.confirm("是否删除这条数据?", operation);
        }
        if (ids1.length > 1) {
            var operation = function () {
                $.ajax(Feng.ctxPath + '/cleanRule/deleteCleanRule', {
                    data: {
                        arr: ids1
                    },
                    //开启序列化
                    traditional: true,
                    success: function (res) {
                        // console.log(res)
                        if (res == ids1.length) {
                            Feng.success('删除成功')
                            table.reload(CleanRule.tableId);
                        }
                    },
                    error: function () {
                        Feng.error('删除失败')
                    }
                })
            };
            Feng.confirm("是否删除这" + ids1.length + "条数据?", operation);
        }
    })


    /**
     * 标准管理
     */
        // 渲染表格
    var tableResult = table.render({
            elem: '#' + StandardManagement.tableId,
            url: Feng.ctxPath + '/standardManagement/list',
            page: true,
            height: "full-158",
            cellMinWidth: 100,
            skin: 'line',
            cols: StandardManagement.initColumn(),
            done: function (res, curr, count) {
                $('tr').css('background-color', '#ffffff')
                //第一次页面进来的时候默认选中业务元数据
                table_data = res.data;
                // console.log(table_data)
            }
        });

    /**
     * 获取表格复选框内的值
     */
    var table_data = new Array();
    var ids = new Array();
    table.on('checkbox(' + StandardManagement.tableId + ')', function (obj) {
        // console.log(obj)
        if (obj.checked == true) {
            if (obj.type == 'one') {
                ids.push(obj.data.dataStandardId);
            } else {
                for (var i = 0; i < table_data.length; i++) {
                    ids.push(table_data[i].dataStandardId);
                }
            }
        } else {
            if (obj.type == 'one') {
                for (var i = 0; i < ids.length; i++) {
                    if (ids[i] == obj.data.dataStandardId) {
                        ids.splice(i, 1);
                    }
                }
            } else {
                for (var i = 0; i < ids.length; i++) {
                    for (var j = 0; j < table_data.length; j++) {
                        if (ids[i] == table_data[j].dataStandardId) {
                            ids.splice(i, 1);
                        }
                    }
                }
            }
        }
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        StandardManagement.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

        StandardManagement.openAddDlg();

    });

    /**
     * 清洗规则
     */
        // 渲染表格
    var tableResult1 = table.render({
            elem: '#' + CleanRule.tableId,
            url: Feng.ctxPath + '/cleanRule/list',
            page: true,
            height: "full-158",
            cellMinWidth: 100,
            skin: 'line',
            cols: CleanRule.initColumn(),
            done: function (res, curr, count) {
                $('tr').css('background-color', '#ffffff')
                //第一次页面进来的时候默认选中业务元数据
                table_data1 = res.data;
                // console.log(table_data)
            }
        });

    /**
     * 获取表格复选框内的值
     */
    var table_data1 = new Array();
    var ids1 = new Array();
    table.on('checkbox(' + CleanRule.tableId + ')', function (obj) {
        // console.log(obj)
        if (obj.checked == true) {
            if (obj.type == 'one') {
                ids1.push(obj.data.dataCleanRuleId);
            } else {
                for (var i = 0; i < table_data1.length; i++) {
                    ids1.push(table_data1[i].dataCleanRuleId);
                }
            }
        } else {
            if (obj.type == 'one') {
                for (var i = 0; i < ids1.length; i++) {
                    if (ids1[i] == obj.data.dataCleanRuleId) {
                        ids1.splice(i, 1);
                    }
                }
            } else {
                for (var i = 0; i < ids1.length; i++) {
                    for (var j = 0; j < table_data1.length; j++) {
                        if (ids1[i] == table_data1[j].dataCleanRuleId) {
                            ids1.splice(i, 1);
                        }
                    }
                }
            }
        }
    });

    // 搜索按钮点击事件
    $('#btnSearch1').click(function () {
        CleanRule.search();
    });

    // 添加按钮点击事件
    $('#btnAdd1').click(function () {

        CleanRule.openAddDlg();

    });

    // 导出excel
    $('#btnExp1').click(function () {
        CleanRule.exportExcel();
    });

});
